public class ASMConfirmDateController
{
     public Activity_Information__c ActivityInfo{get;set;}
     public CommonAttribute CommonAtt{get;set;}
     public String OutletName{get;set;}
     public CommonMethod getCommonMethod;
     public CommonRecursionMethod getRecursionMethod;
      public Participator_Detail_Area__c parDetailArea;
     public List<SelectOption> ParticipatorKAList{get;set;}//参与列表中的KA
     public Boolean IsDisplayForComfirmDate{get;set;}
     public Boolean IsKAOrRegionActivity{get;set;}//进入该页面的活动类型是KA还是Region
     public string SubordinateUserId{get;set;}//获取当前用户下面的所有用户
     public List<id> SubordinateUserIdList{get;set;}//获取当前用户下面的所有用户
     public List<SelectOption> CityList{get;set;}//城市列表
     public Integer outletNumber{get;set;}
     public String CompetitionType{get;set;}
     public List<Activity_Competition_Brand_Detail__c> ETCompetitionList{get;set;}
      public List<Activity_Competition_Brand_Detail__c> FGCompetitionList{get;set;}
     public User user
     {
         get{return [select id,UserRegion__c from User where id=:UserInfo.getUserId()];}
     }
     public String RecordId
     {
           get
           {
               return String.valueOf(System.currentPageReference().getParameters().get('id')).substring(0,15);  
           }
           set;
     }
     //活力类型
        public String activityType
     {
           get
           {
               return String.valueOf(System.currentPageReference().getParameters().get('activityType'));  
           //   return 'KA';
           }
           set;
     }
  
   public ASMConfirmDateController(ApexPages.StandardController controller)
     {
         IsDisplayForComfirmDate=false;
         getCommonMethod=new CommonMethod();
         CommonAtt=new CommonAttribute ();
         getRecursionMethod=new CommonRecursionMethod();
         
         if(CityList==NULL)
             CityList=new List<SelectOption>();
         else
             CityList.clear();
         
         if(ParticipatorKAList==NULL)
             ParticipatorKAList=new List<SelectOption>();
         else
             ParticipatorKAList.clear();      
                 //获取当前用户下的子用户SubordinateUserId 
             SubordinateUserIdList=new List<id>(getRecursionMethod.getASMBelowRoleId());
             SubordinateUserId ='';
               for(Id idString:SubordinateUserIdList)
               {
                   if(SubordinateUserId=='')
                       SubordinateUserId='\''+idString+'\'';
                    else
                    {
                        SubordinateUserId+=',';
                        SubordinateUserId+='\''+idString+'\'';
                    }
               }
      
         map<string,List<SelectOption>> KAAndCityList=getCommonMethod.getSubordinateCity(RecordId,SubordinateUserIdList);
         if(KAAndCityList!=NULL)
         {
             if(KAAndCityList.get('City')!=NULL)
                 CityList=KAAndCityList.get('City');
             if(KAAndCityList.get('KA')!=NULL)
                 ParticipatorKAList=KAAndCityList.get('KA');
         }
        
          IsKAOrRegionActivity=true;
         if(activityType=='Region')
            IsKAOrRegionActivity=false;

         
             ActivityInfo=[Select Id,Name,  Activity_End_Date__c, Activity_Notice__c, Activity_Stage__c, Activity_Start_Date__c, 
                               Activity_Type_State__c, Activity_Type__c,Serial_Number__c,Regional_Activity_Type__c 
                               FROM Activity_Information__c where id=: RecordId]; 
         
           if(ActivityInfo.Activity_Type__c!='SSY')
               CompetitionType='竞品费用投入资料';
            else
               CompetitionType='KA竞品费用投入资料';
               
         
         //查出生效的审批记录
             List<Participator_Detail_Area__c> parDetailAreaList=[select id,Submit_ASM__c,Outlet_Amount__c,Approve_Type__c,Approve_Description__c 
                                                                                                    from Participator_Detail_Area__c where Activity_Information__c=:RecordId  and 
                                                                                                    Submit_ASM__c=:UserInfo.getUserId()  and IsActive__c=true limit 1];
             if(parDetailAreaList!=NULL && parDetailAreaList.size()>0)
                 parDetailArea=parDetailAreaList[0];  
             
           outletNumber=[select count() from Participator_Detail_Outlet__c where ActivityId__c=:RecordId and OwnerId in:SubordinateUserIdList];     
           if(ActivityInfo.Activity_Type__c=='KA'){
                 if(parDetailArea!=null && parDetailArea.Approve_Type__c=='ASM已提交')
                   IsDisplayForComfirmDate=true;
           } else{
              if(parDetailArea!=null && parDetailArea.Approve_Type__c=='SRTM已审批' && parDetailArea.Approve_Description__c=='同意')
                  IsDisplayForComfirmDate=true;
           }
              
      
          if(CommonAtt.CitySelectedValue==NULL && CityList.size()>0)
             CommonAtt.CitySelectedValue=CommonAtt.AllCityList[0].getValue();
          if(CommonAtt.KASelectedValue==NULL && ParticipatorKAList.size()>0 && ActivityInfo.Activity_Type__c!='Region')
              CommonAtt.KASelectedValue=ParticipatorKAList[0].getValue();
          LoadCompetitionList('KA');//加载KA竞品信息
          if(ActivityInfo.Activity_Type__c=='SSY')
             LoadCompetitionList('OT');//加载KA竞品信息  
          
     }
   
 
     public PageReference OutletQuery()
     {
         LoadCompetitionList('KA');//加载ET,FG竞品信息
         if(ActivityInfo.Activity_Type__c=='SSY')
             LoadCompetitionList('OT');//加载KA竞品信息     
         return null;
     }
   
     //加载竞品信息
     public void LoadCompetitionList(String typeStr)
     {
         String filter=GetSqlFilter(typeStr);
         String sql='select OutletName__c,SubmitASM__c,OutletId__c,Promotion_Type__c,Cost_Type__c,Competition_Cost_Source__c,'+
                            ' Money__c,Start_Date__c,End_Date__c,Display_Type__c from Activity_Competition_Brand_Detail__c'+
                            '  where ActivityId__c=\''+RecordId +'\''
                            +filter;
              //             throw new CustomException (sql);  
    
            if(typeStr=='KA'){
             ETCompetitionList=Database.Query(sql);
            }
            else  {
             FGCompetitionList=Database.Query(sql);     
           
             }        
     }
     
     public String GetSqlFilter(String typeStr)
     {
          if(CommonAtt.CitySelectedValue==NULL)
             CommonAtt.CitySelectedValue=CommonAtt.AllCityList[0].getValue();
          if(CommonAtt.KASelectedValue==NULL)
              CommonAtt.KASelectedValue=CommonAtt.OutletKAList[0].getValue();
                //and Participator_Detail_Outlet__r.Participator_Area__r.KA_Status__c=\'KAM已提交\'
        String filter=' and Participator_Detail_Outlet__r.City__c=\''+CommonAtt.CitySelectedValue+'\'';
        if(OutletName!=NULL)
            filter+=' and Participator_Detail_Outlet__r.Outlet_Name__r.Name like \'%'+OutletName+'%\'';
        if(ActivityInfo.Activity_Type__c=='KA' || ActivityInfo.Activity_Type__c=='SSY'){
            if(OutletName!=NULL)
                  filter+=' and Participator_Detail_Outlet__r.Outlet_Name__r.Name like \'%'+OutletName+'%\'';
            if(CommonAtt.OutletTypeSelectedValue!='' && CommonAtt.OutletTypeSelectedValue!=NULL)
                 filter+=' and Participator_Detail_Outlet__r.OutletType__c=\''+CommonAtt.OutletTypeSelectedValue+'\'';
            if(CommonAtt.ChannelSelectedValue!='' && CommonAtt.ChannelSelectedValue!=NULL)
                 filter+=' and Participator_Detail_Outlet__r.Channel__c=\''+CommonAtt.ChannelSelectedValue+'\'';
             if(typeStr=='KA')   
                filter+=' and Participator_Detail_Outlet__r.KA__c=\''+CommonAtt.KASelectedValue+'\'';
            else
                filter+=' and Participator_Detail_Outlet__r.OutletResourceType__c=\'OT\'';
                   
      //      filter+=' and Participator_Detail_Outlet__r.KA__c=\''+CommonAtt.KASelectedValue+'\'';
        }
         
                     //  filter+=' and Participator_Detail_Outlet__r.OutletResourceType__c=\'OT\'';
            //  filter+=' and Participator_Detail_Outlet__r.Activity_Stage__c=\'SRTM已提交\'';
           if(SubordinateUserId!=NULL && SubordinateUserId.length()>0)
                filter+=' and Participator_Detail_Outlet__r.OwnerId in ('+SubordinateUserId+')';
         return filter;
     }
    
    
     public PageReference SaveActivity()
     {
        SaveData();
    //     PageReference pr=new PageReference('/'+RecordId );
      //   pr.setRedirect(true);
         return null;
     }
    
     public void SaveData()
     {
             //保存竞品信息
         //    if(parId!=NULL)
        //     {   
                 for(Activity_Competition_Brand_Detail__c acbd:ETCompetitionList)
                 {
                   if(acbd.Money__c!=NULL){
                     if(acbd.Start_Date__c==NULL || acbd.End_Date__c==NULL)
                         throw new CustomException('竞品设置了金额，则开始时间、结束时间为必填项');         
                   }         
                 }
             
       //      }
            update ETCompetitionList;     
            if(ActivityInfo.Activity_Type__c=='SSY')    
                  update FGCompetitionList;                         
     }
     public PageReference ConfirmDate()
     { 
         parDetailArea.Approve_Type__c='ASM已确认时间';
         update parDetailArea;
         SaveData();
         PageReference pr=new PageReference('/'+RecordId );
          pr.setRedirect(true);
          return pr;
     }
}